import type {
  CartResult,
  LogisticsResult,
  OrderDetailResult,
  OrderList,
  OrderListParams,
  OrderResult,
  PostOrder,
} from '@/types/cart'
import { http } from '@/utils/https'
import type { OrderState } from './constance'

export const postCartAPI = (data: { skuId: string; count: number }) => {
  return http<CartResult>({
    method: 'POST',
    url: '/member/cart',
    data,
  })
}
export const getCartAPI = () => {
  return http<CartResult[]>({
    url: '/member/cart',
  })
}

export const deleteCartAPI = (data: { ids: string[] }) => {
  return http({
    method: 'DELETE',
    url: '/member/cart',
    data,
  })
}

export const putCartAPI = (skuId: string, data: { count?: number; selected?: boolean }) => {
  return http({
    method: 'PUT',
    url: `/member/cart/${skuId}`,
    data,
  })
}

export const putCartCheckAPI = (data: { selected: boolean }) => {
  return http({
    method: 'PUT',
    url: '/member/cart/selected',
    data,
  })
}
type OrderParams = {
  skuId: string
  count: string
  addressId?: string
}

export const getOrderPreNowAPI = (data: OrderParams) => {
  return http<OrderResult>({
    url: '/member/order/pre/now',
    data,
  })
}

export const getOrderAPI = () => {
  return http<OrderResult>({
    url: '/member/order/pre',
  })
}

//再次购买
export const getOrderRepurchaseAPI = (id: string) => {
  return http<OrderResult>({
    url: `/member/order/repurchase/${id}`,
  })
}

export const postOrderAPI = (data: PostOrder) => {
  return http<{ id: string }>({
    method: 'POST',
    url: '/member/order',
    data,
  })
}

export const getOrderDetailAPI = (id: string) => {
  return http<OrderDetailResult>({
    url: `/member/order/${id}`,
  })
}

export const putOrderCancleAPI = (id: string, data: { cancelReason: string }) => {
  return http<OrderDetailResult>({
    method: 'PUT',
    url: `/member/order/${id}/cancel`,
    data,
  })
}
//支付
export const PayWxpayAPI = (data: { orderId: string }) => {
  return http<WechatMiniprogram.RequestPaymentOption>({
    url: '/pay/wxPay/miniPay',
    data,
  })
}

export const PayMockAPI = (data: { orderId: string }) => {
  return http({
    url: '/pay/mock',
    data,
  })
}

//模拟发货
export const OrderConsignmentAPI = (id: string) => {
  return http({
    url: `/member/order/consignment/${id}`,
  })
}

//确认收货
export const OrderReceiptAPI = (id: string) => {
  return http<OrderDetailResult>({
    method: 'PUT',
    url: `/member/order/${id}/receipt`,
  })
}

//获取订单物流
export const getOrderLogisticsAPI = (id: string) => {
  return http<LogisticsResult>({
    url: `/member/order/${id}/logistics`,
  })
}
//删除订单
export const deleteOrderAPI = (data: { ids: string[] }) => {
  return http({
    method: 'DELETE',
    url: '/member/order',
    data,
  })
}

//获取订单列表
export const getOrderListAPI = (data: OrderListParams) => {
  return http<OrderList>({
    url: '/member/order',
    data,
  })
}
